<?php
/**
 * 核心Db框架
 * @author sunhaoqiang(shqdev@163.com)
 * @time: 2019-08-25 16:38
*/
namespace app\item;
class Paper
{
    public function paperSingle($paper)
    {
        if (empty($paper)) {
            return [];
        }
        $paper = $this->formatField($paper);
        $data = [
            "xd" => $paper["xd"],
            "chid" => $paper["chid"],
            "uid" => $paper["uid"],
            "title" => $paper["title"],
            "style" => $paper["style"],
            "question_ids" => $paper["question_ids"],
            "tizu_sort" => $paper["tizu_sort"],
            "pid" => $paper["pid"],
            "download_num" => $paper["download_num"],
            "look_num" => $paper["look_num"],
            "paper_uid" => $paper["paper_uid"],
            "paper_type" => $paper["paper_type"],
            "add_time" => $paper["addtime"],
            "edit_time" => $paper["edit_time"],
            "examine_time" => $paper["examine_time"],
            "question_num" => $paper["question_num"],
            "examtime" => $paper["examtime"],
            "notes" => $paper["notes"],
            "part_name" => $paper["part_name"],
            "part_tip" => $paper["part_tip"],
            "score" => $paper["score"],
            "subtitle" => $paper["subtitle"],
            "part_count" => $paper["part_count"],
            "is_collect" => $paper["is_collect"],
            "flag" => $paper["flag"],
            "short_title" => $paper["short_title"],
        ];
        return $data;
    }
    public function tizuSingle($tizu)
    {
        $tizu = (array)$tizu;
        if (empty($tizu)) {
            return [];
        }
        $tizu = $this->formatField($tizu);

        $data = [
            'paper_id' => $tizu['paper_id'],
            'head_title' => $tizu['head_title'],
            'scores' => isset($tizu['scores'])? json_encode($tizu['scores']) :'',
            'question_ids' => isset($tizu['scores'])? implode(',', array_keys((array)$tizu['scores'])): '',
            "add_time" => date('Y-m-d H:i:s'),
        ];
        return $data;
    }

    /**
     * 转换双引号和单引号
     */

    public function escape($str)
    {
        return htmlspecialchars($str, ENT_QUOTES);
    }
    /**
     * 格式化字段
     */
    public function formatField($data)
    {
        if (empty($data)) {
            return [];
        }
        foreach ($data as $key => $value) {
            if (is_string($data[$key])) {
                $data[$key] = $this->escape($data[$key]);
            }elseif (is_array($data[$key])) {
                $data[$key] = $this->escape(json_encode($data[$key]));
            }elseif (is_object($data[$key])){
                $data[$key] = $this->escape(json_encode($data[$key]));
            }
        }
        return $data;
    }

    public function questionSingle($question)
    {
        // htmlspecialchars($str, ENT_QUOTES);
        $question = (array)$question;

        if (empty($question)) {
            return [];
        }
        $question = $this->formatField($question);

        $data = [
            "question_id" => $question["question_id"],
            "question_type" => $question["question_type"],
            "question_channel_type" => $question["question_channel_type"],
            "channel_type_name" => $question["channel_type_name"],
            "title" => $question["title"],
            "question_text" => $question["question_text"],
            "options" => ($question["options"]),
            "answer_json" => $question["answer_json"],
            "answer" => $question["answer"],
            "explanation" => $question["explanation"],
            "knowledge" => $question["knowledge"],
            "t_knowledge" => $question["t_knowledge"],
            "category" => $question["category"],
            "parent_id" => $question["parent_id"],
            "paperid" => $question["paperid"],
            "xd" => $question["xd"],
            "chid" => $question["chid"],
            "exam_type" => $question["exam_type"],
            "exam_name" => $question["exam_name"],
            "difficult_index" => $question["difficult_index"],
            "difficult_name" => $question["difficult_name"],
            "is_objective" => $question["is_objective"],
            "is_collect" => $question["is_collect"],
            "extra_file" => $question["extra_file"],
            "save_num" => $question["save_num"],
            "new_flag" => $question["new_flag"],
            "is_use" => $question["is_use"],
            "question_source" => $question["question_source"],
            "score" => isset($question["score"])? $question["score"] :'',
            "subScore" => isset($question["subScore"])? $question["subScore"] :'',
            "list" => isset($question["list"])? $question["list"] :'',
            "add_time" => date('Y-m-d H:i:s'),
        ];
        return $data;
    }
    
}